package com.reset.password.result;

/**
 * 全平台统一响应结果
 */
public class Result<T> {

    private Integer code;
    private String msg;
    private T data;

    // 构造函数
    protected Result() {}

    // Getter and Setter
    public Integer getCode() {
        return code;
    }
    public Result code(Integer code) {
        this.code = code;
        return this;
    }
    public String getMsg() {
        return msg;
    }
    public Result msg(String msg) {
        this.msg = msg;
        return this;
    }
    public T getData() {
        return data;
    }
    public Result data(T data) {
        this.data = data;
        return this;
    }

    // 静态工厂方法
    public static <T> Result<T> success(T data) {
        return new ResultBuilder<>(ResultCode.SUCCESS, data);
    }
    public static <T> Result<T> success() {
        return new ResultBuilder<>(ResultCode.SUCCESS, null);
    }
    public static <T> Result<T> error(ResultCode code) {
        return new ResultBuilder<>(code);
    }
    public static <T> Result<T> error() {
        return new ResultBuilder<>(ResultCode.ERROR, "异常");
    }
    public static <T> Result<T> error(ResultCode code, String msg, T data) {
        return new ResultBuilder<>(code, msg, data);
    }

    // 内部构建器类
    protected static class ResultBuilder<T> extends Result<T> {
        private ResultBuilder(ResultCode code) {
            code(code.getCode());
            msg(code.getMessage());
        }
        private ResultBuilder(ResultCode code, T data) {
            code(code.getCode());
            msg(code.getMessage());
            data(data);
        }
        private ResultBuilder(ResultCode code, String msg) {
            code(code.getCode());
            msg(msg);
        }
        private ResultBuilder(ResultCode code, String msg, T data) {
            code(code.getCode());
            msg(msg);
            data(data);
        }
    }
}
